unit UMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, RXCtrls, ExtCtrls, ToolWin, ComCtrls, RXSplit, Menus, RxMenus,
  Db, ADODB, WiADOConnection, ActnList;

const
  NazwaOkna = 'Query ';

type
  TFMain = class(TForm)
    ToolBar1: TToolBar;
    Bevel1: TBevel;
    RxSpeedButton1: TRxSpeedButton;
    BtNowy: TRxSpeedButton;
    TV: TTreeView;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Okna1: TMenuItem;
    Zamknij1: TMenuItem;
    Kaskada1: TMenuItem;
    RxSplitter1: TRxSplitter;
    Ssiadujco1: TMenuItem;
    Baza1: TMenuItem;
    Bazy1: TMenuItem;
    Tabele1: TMenuItem;
    Procedury1: TMenuItem;
    N1: TMenuItem;
    Funkcje1: TMenuItem;
    View1: TMenuItem;
    Ssiadujcohoryzontalnie1: TMenuItem;
    N2: TMenuItem;
    Nowy1: TMenuItem;
    Otwrz1: TMenuItem;
    Label1: TLabel;
    cbBazy: TComboBox;
    Dynamics: TWiADOConnection;
    QFunc: TADOQuery;
    ActionList1: TActionList;
    RxSplitter2: TRxSplitter;
    lbObjekty: TTextListBox;
    btObiekty: TRxSpeedButton;
    Action1: TAction;
    Memo1: TMemo;
    qTemp: TADOQuery;
    procedure RxSpeedButton1Click(Sender: TObject);
    procedure Nowy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TVChange(Sender: TObject; Node: TTreeNode);
    procedure Kaskada1Click(Sender: TObject);
    procedure Ssiadujco1Click(Sender: TObject);
    procedure Ssiadujcohoryzontalnie1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure cbBazyChange(Sender: TObject);
    procedure Tabele1Click(Sender: TObject);
    procedure btObiektyClick(Sender: TObject);
    procedure lbObjektyDblClick(Sender: TObject);
    procedure View1Click(Sender: TObject);
    procedure Procedury1Click(Sender: TObject);
    procedure Funkcje1Click(Sender: TObject);
  private
    { Private declarations }
    LicznikOkien : Integer;
    TypObjektu : Char;
    procedure PokazTabele;
    procedure PokazView;
    procedure PokazFunkcje;
    procedure PokazProcedure;
  public
    { Public declarations }
    procedure ZamknijOkno(ACap: String);
    procedure AktualizujOkno (ACap, ASQL: String); 
  end;

var
  FMain: TFMain;

implementation

{$R *.DFM}

uses UQuery, Wilk;

procedure TFMain.RxSpeedButton1Click(Sender: TObject);
begin
  Close;
end;

procedure TFMain.Nowy(Sender: TObject);
var
  okno : TFQuery;
begin
  Inc(LicznikOkien);
  okno := TFQuery.Create(Application);
  okno.Caption := NazwaOkna+IntToStr(LicznikOkien);
  okno.Query.Connection := Dynamics;
  TV.Items.Add(nil, NazwaOkna+IntToStr(LicznikOkien));
end;

procedure TFMain.FormCreate(Sender: TObject);
begin
  LicznikOkien := 0;
  Dynamics.Open;
end;

procedure TFMain.ZamknijOkno(ACap: String);
var
  Nr : Integer;
  i  : Integer;
begin
  Nr := StrToInt(Copy(ACap,Length(NazwaOkna),Length(ACap)));
  if Nr=LicznikOkien then
    Dec(LicznikOkien);
  i := 0;
  while (i<TV.Items.Count) and (TV.Items[i].Text<>ACap) do
    inc(i);
  if (i<TV.Items.Count) then
     TV.Items[i].Delete;
end;

procedure TFMain.TVChange(Sender: TObject; Node: TTreeNode);
var
  i : integer;
  t : String;
begin
  if (Node.Level=0)  then
    t := Node.Text
  else
    t := Node.Parent.Text;
  i := 0;
  while (i<MDIChildCount) and (MDIChildren[i].Caption<>t) do
     Inc(i);
  if (i<MDIChildCount) then
    MDIChildren[i].Show;
end;

procedure TFMain.AktualizujOkno(ACap, ASQL: String);
var
  i  : Integer;
begin
  i := 0;
  while (i<TV.Items.Count) and (TV.Items[i].Text<>ACap) do
    inc(i);
  if (i<TV.Items.Count) then begin
    if TV.Items[i].HasChildren then begin
      TV.Items[i].Item[0].Text := ASQL;
    end else
      TV.Items.AddChild(TV.Items[i], ASQL);
    TV.Items[i].Expand(True);
  end;
end;

procedure TFMain.Kaskada1Click(Sender: TObject);
begin
  Cascade;
end;

procedure TFMain.Ssiadujco1Click(Sender: TObject);
begin
  TileMode := tbVertical;
  Tile;
end;

procedure TFMain.Ssiadujcohoryzontalnie1Click(Sender: TObject);
begin
  TileMode := tbHorizontal;
  Tile;
end;

procedure TFMain.FormActivate(Sender: TObject);
begin
  with QFunc do begin
    Close;
    SQL.Clear;
//    SQL.Add('exec sp_helpdb');
    SQL.Add('select name from master.dbo.sysdatabases');
    Open;
    cbBazy.Items.Clear;
    while not EOF do begin
      cbBazy.Items.Add(FieldByName('Name').AsString);
      Next;
    end;
    cbBazy.Text := Dynamics.DefaultDatabase;
    Close;
  end;
end;

procedure TFMain.cbBazyChange(Sender: TObject);
begin
  Dynamics.Close;
  Dynamics.DefaultDatabase := cbBazy.Text;
  Dynamics.Connected := True;
end;

procedure TFMain.Tabele1Click(Sender: TObject);
begin
  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('select name from sysobjects where xtype=''U''');
    Open;
    lbObjekty.Items.Clear;
    while not EOF do begin
      lbObjekty.Items.Add(Trim(Fields[0].AsString));
      Next;
    end;
    Close;
  end;
  lbObjekty.Visible := True;
  RxSplitter2.Visible := True;
  btObiekty.Visible := True;
  TypObjektu := 'U';
end;

procedure TFMain.btObiektyClick(Sender: TObject);
begin
  RxSplitter2.Visible := False;;
  lbObjekty.Visible := False;
  btObiekty.Visible := False;
end;

procedure TFMain.lbObjektyDblClick(Sender: TObject);
begin
  case TypObjektu of
    'U' : PokazTabele;
    'V' : PokazView;
    'F' : PokazFunkcje;
    'P' : PokazProcedure;
  end;
end;

procedure TFMain.PokazTabele;
var
  okno : TFQuery;
  S , S2: String;
  i : integer;
begin
  okno := TFQuery.Create(Application);
  okno.Caption := NazwaOkna+IntToStr(LicznikOkien);
  okno.Query.Connection := Dynamics;
  TV.Items.Add(nil, NazwaOkna+IntToStr(LicznikOkien));
  Okno.Memo1.Lines.Text := 'SELECT TOP 100 * FROM '+lbObjekty.Items[lbObjekty.ItemIndex];
  Okno.btExec.ButtonClick;
end;

procedure TFMain.PokazView;
var
  okno : TFQuery;
  S , S2: String;
  i : integer;
begin
  //SQLOpen(qTemp, 'exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
  SQLOpen(qTemp, 'SELECT OBJECT_DEFINITION(OBJECT_ID('''+lbObjekty.Items[lbObjekty.ItemIndex]+'''))');
  if not qTemp.IsEmpty then
  begin
    s := qTemp.Fields[0].AsString;
{    s := '';
    while not qTemp.EOF do
    begin
      s := s + qTemp.Fields[0].AsString;
      qTemp.Next;
    end;}
    Inc(LicznikOkien);
    okno := TFQuery.Create(Application);
    okno.Caption := NazwaOkna+IntToStr(LicznikOkien);
    okno.Query.Connection := Dynamics;
    TV.Items.Add(nil, NazwaOkna+IntToStr(LicznikOkien));
    Okno.Memo1.Lines.Text := s;
  end;

{  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
    Open;
    s := '';
    while not EOF do begin
      s := S + Fields[0].AsString;
      Next;
    end;
    okno.Memo1.Lines.Text := (s);
    Close;
  end;}
//  Okno.Memo1.Lines.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
//  Okno.StartExecute(nil);
//  Okno.Memo1Change(self);
end;

procedure TFMain.View1Click(Sender: TObject);
begin
  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('select name from sysobjects where xtype=''V''');
    Open;
    lbObjekty.Items.Clear;
    while not EOF do begin
      lbObjekty.Items.Add(Trim(Fields[0].AsString));
      Next;
    end;
    Close;
  end;
  lbObjekty.Visible := True;
  RxSplitter2.Visible := True;
  btObiekty.Visible := True;
  TypObjektu := 'V';
end;


procedure TFMain.Procedury1Click(Sender: TObject);
begin
  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('select name from sysobjects where xtype=''P''');
    Open;
    lbObjekty.Items.Clear;
    while not EOF do begin
      lbObjekty.Items.Add(Trim(Fields[0].AsString));
      Next;
    end;
    Close;
  end;
  lbObjekty.Visible := True;
  RxSplitter2.Visible := True;
  btObiekty.Visible := True;
  TypObjektu := 'P';
end;

procedure TFMain.Funkcje1Click(Sender: TObject);
begin
  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('select name from sysobjects where xtype=''FN''');
    Open;
    lbObjekty.Items.Clear;
    while not EOF do begin
      lbObjekty.Items.Add(Trim(Fields[0].AsString));
      Next;
    end;
    Close;
  end;
  lbObjekty.Visible := True;
  RxSplitter2.Visible := True;
  btObiekty.Visible := True;
  TypObjektu := 'F';
end;

procedure TFMain.PokazFunkcje;
var
  okno : TFQuery;
  S , S2: String;
  i : integer;
begin
  SQLOpen(qTemp, 'exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
  if not qTemp.IsEmpty then
  begin
    s := '';
    while not qTemp.EOF do
    begin
      s := s + qTemp.Fields[0].AsString;
      qTemp.Next;
    end;
    Inc(LicznikOkien);
    okno := TFQuery.Create(Application);
    okno.Caption := NazwaOkna+IntToStr(LicznikOkien);
    okno.Query.Connection := Dynamics;
    TV.Items.Add(nil, NazwaOkna+IntToStr(LicznikOkien));
    Okno.Memo1.Lines.Text := s;
  end;
{  with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
    Open;
    s := '';
    while not EOF do begin
    s := Fields[0].AsString;
    s2 := '';
      //Memo1.Lines.Add(StrReplace(StrReplace(Fields[0].AsString, #10, ''), #09, '   '));
      for i := 1 to Length(s) do
      begin
        s2 := s2 + s[i]+' '+IntToStr(Ord(s[i]))+'.';
      end;
      Memo1.Lines.Add(s2);
   //   s := S + Fields[0].AsString;
      Next;
    end;
//    okno.Memo1.Lines.Text := (copy(s, 1, Length(s)-10));
    Close;
  end;}

//  Okno.Memo1.Lines.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
//  Okno.StartExecute(nil);
//  Okno.Memo1Change(self);
end;

procedure TFMain.PokazProcedure;
var
  okno : TFQuery;
  S , S2: String;
  i : integer;
begin
  SQLOpen(qTemp, 'exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
  if not qTemp.IsEmpty then
  begin
    s := '';
    while not qTemp.EOF do
    begin
      s := s + qTemp.Fields[0].AsString;
      qTemp.Next;
    end;
  Inc(LicznikOkien);
  okno := TFQuery.Create(Application);
  okno.Caption := NazwaOkna+IntToStr(LicznikOkien);
  okno.Query.Connection := Dynamics;
  TV.Items.Add(nil, NazwaOkna+IntToStr(LicznikOkien));

  Okno.Memo1.Lines.Text := s;
 { with QFunc do begin
    Close;
    SQL.Clear;
    SQL.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
    Open;
    s := '';
    while not EOF do begin
      s := S + Fields[0].AsString;
      Next;
    end;
    okno.Memo1.Lines.Add(s);
    Close;
  end;}
//  Okno.Memo1.Lines.Add('exec sp_HelpText '+lbObjekty.Items[lbObjekty.ItemIndex]);
//  Okno.StartExecute(nil);
//  Okno.Memo1Change(self);
  end;
end;

end.
